﻿
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Kaiser.Utilities;


namespace Kaiser.Models
{
    /// <summary>
    /// 菜单类  add by liubd
    /// </summary>
    public class KasierMenus
    {
        public string menuid { get; set; }

        public string icon { get; set; }

        public string menuname { get; set; }

        public string url { get; set; }

        private IList<KasierMenus> _menus;

        public IList<KasierMenus> menus
        {
            get { return _menus ?? (_menus = new List<KasierMenus>()); }
            set { _menus = value; }
        }

        public static void GetElements(ref List<KasierMenus> refMenus, KasierMenus parent, IList<XElement> elements)
        {
            foreach (var item in elements)
            {
                var menu = new KasierMenus()
                    {
                        icon = XmlHelper.GetElementAttrVal(item, "icon"),
                        menuid = XmlHelper.GetElementAttrVal(item, "menuid"),
                        menuname = XmlHelper.GetElementAttrVal(item, "menuname"),
                        url = XmlHelper.GetElementAttrVal(item, "url")
                    };
                if (parent != null)
                {
                    parent.menus.Add(menu);
                }
                else
                {
                    refMenus.Add(menu);
                }
                GetElements(ref refMenus, menu, item.Elements().ToList());
            }
        }

        public static List<KasierMenus> GetAllMenus(string xmlPath, string rootName)
        {
            var xml = new XmlHelper(xmlPath);
            var res = xml.GetElementByParent(xml.GetElementByName(rootName));
            var menus = new List<KasierMenus>();
            GetElements(ref menus, null, res.ToList());
            return menus;
        }
    }
}
